package org.example.websockettest.utils;

import org.example.websockettest.enums.MessageEnums;

/**
 * 标准化API响应体
 *
 * @param <T> 响应中包含的数据类型
 */
public class ApiResponse<T> {
    private int status;
    private String message;
    private T data;
    private boolean success;

    /**
     * ApiResponse的构造方法。
     *
     * @param status HTTP状态码
     * @param message 描述响应的消息
     * @param data 包含在响应中的数据
     * @param success 表示操作是否成功的标志
     */
    public ApiResponse(int status, String message, T data, boolean success) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    // Getter和Setter方法
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    // 使用MessageEnums的静态工厂方法
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(MessageEnums.OK.getCode(), MessageEnums.OK.getMessage(), data, true);
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(MessageEnums.OK.getCode(),message, data, true);
    }

    public static <T> ApiResponse<T> error(MessageEnums messageEnum) {
        return new ApiResponse<>(messageEnum.getCode(), messageEnum.getMessage(), null, false);
    }
    public static <T> ApiResponse<T> error(Integer code,String message) {
        return new ApiResponse<>(code,message, null, false);
    }

    public static <T> ApiResponse<T> custom(MessageEnums messageEnum, T data, boolean success) {
        return new ApiResponse<>(messageEnum.getCode(), messageEnum.getMessage(), data, success);
    }

    // 允许自定义消息内容的静态工厂方法
    public static <T> ApiResponse<T> custom(int status, String message, T data, boolean success) {
        return new ApiResponse<>(status, message, data, success);
    }

    // 常用响应的额外辅助方法
    public static <T> ApiResponse<T> notFound(String customMessage) {
        return custom(MessageEnums.NOT_FOUND.getCode(), customMessage, null, false);
    }

    public static <T> ApiResponse<T> forbidden(String customMessage) {
        return custom(MessageEnums.FORBIDDEN.getCode(), customMessage, null, false);
    }
}
